package com.gwtintegration.smartrfsandbox.server;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.web.bindery.requestfactory.server.RequestFactoryServlet;
import com.google.web.bindery.requestfactory.shared.ServiceLocator;

public class SpringServiceLocator implements ServiceLocator {

	@Override
	public Object getInstance(Class<?> clazz) {
		HttpServletRequest request = RequestFactoryServlet.getThreadLocalRequest();
		ServletContext servletContext = request.getSession().getServletContext();
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		return context.getBean(clazz);
	}
}